package com.kongxx.freemarkstudy.directive.macro;

import java.io.File;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.kongxx.freemarkstudy.model.User;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

public class MacroExample {
	public static void main(String[]args) throws Exception {
		// Create a configuration instance
		Configuration cfg = new Configuration();
		File file = new File(MacroExample.class.getResource("").getFile());
		cfg.setDirectoryForTemplateLoading(file);
		cfg.setObjectWrapper(new DefaultObjectWrapper());
		
		// Create a data model
		Map root = new HashMap();
		List<User> users = new ArrayList<User>();
		for(int i = 0 ; i < 10 ; i++) {
			users.add(new User("user"+i, "password"+i));
		}
		root.put("users", users);
		
		// Get the template
		Template temp = cfg.getTemplate("main.ftl"); 

		// Merging the template with the data model
		Writer out = new OutputStreamWriter(System.out);
		temp.process(root, out);
		out.flush(); 
	}
}
